package com.bangcommunity.bbframe.mlog;

import com.bangcommunity.bbframe.common.base.attribute.CommonAttributeAssemble;
import com.bangcommunity.bbframe.common.base.attribute.CommonAttributeDesc;
import com.bangcommunity.bbframe.common.base.attribute.IAttributeAssemble;
import com.bangcommunity.bbframe.common.utils.lang.StringUtils;

/**
 * @author tanghc
 * @date 18/1/25
 */
public class LogContext {
    private String threadName;
    private String txId;
    private String ptxId;
    private String mkey;
    private String bizKey;
    private Object[] args;
    private long startTimeMs;
    private long stopTimeMs;
    private long concurrent;

    public String getThreadName() {
        return threadName;
    }

    public void setThreadName(String threadName) {
        this.threadName = threadName;
    }

    public String getTxId() {
        return txId;
    }

    public void setTxId(String txId) {
        this.txId = txId;
    }

    public String getPtxId() {
        return ptxId;
    }

    public void setPtxId(String ptxId) {
        this.ptxId = ptxId;
    }

    public String getMkey() {
        return mkey;
    }

    public void setMkey(String mkey) {
        this.mkey = mkey;
    }

    public String getBizKey() {
        return bizKey;
    }

    public void setBizKey(String bizKey) {
        this.bizKey = bizKey;
    }

    public Object[] getArgs() {
        return args;
    }

    public void setArgs(Object[] args) {
        this.args = args;
    }

    public long getStartTimeMs() {
        return startTimeMs;
    }

    public long getConcurrent() {
        return concurrent;
    }

    public void setConcurrent(long concurrent) {
        this.concurrent = concurrent;
    }

    public void setStartTimeMs(long startTimeMs) {
        this.startTimeMs = startTimeMs;
    }

    public long getStopTimeMs() {
        return stopTimeMs;
    }

    public void setStopTimeMs(long stopTimeMs) {
        this.stopTimeMs = stopTimeMs;
    }

    // public String toLogRecordString(){
    // return new StringBuilder()
    // .append(threadName).append(", ")
    // .append(System.currentTimeMillis() - startTimeMs).append("ms, ")
    // .append(StringUtils.trimToEmpty(ptxId)).append(", ")
    // .append(StringUtils.trimToEmpty(txId)).append(", ")
    // .append(StringUtils.trimToEmpty(mkey)).append(", ")
    // .append(StringUtils.trimToEmpty(bizKey)).append(", [")
    // .append(StringUtils.join(args, ", ")).append("]").toString();
    // }
    public IAttributeAssemble toAttributeAssemble() {
        return new CommonAttributeAssemble().add(new CommonAttributeDesc("time", System.currentTimeMillis()))
                .add(new CommonAttributeDesc("threadName", this.getThreadName(), true))
                .add(new CommonAttributeDesc("totalTimeMs", stopTimeMs - startTimeMs))
                .add(new CommonAttributeDesc("ptxId", this.getPtxId(), true))
                .add(new CommonAttributeDesc("txId", this.getTxId(), true))
                .add(new CommonAttributeDesc("mkey", this.getMkey(), true))
                .add(new CommonAttributeDesc("bizKey", this.getBizKey(), true))
                .add(new CommonAttributeDesc("args", "[" + StringUtils.join(args, ", ") + "]", true));
    }

    // public String toLogRecordJsonString() {
    // return new StringBuilder(128)
    // .append("{")
    // .append("\"time\"").append(":").append(System.currentTimeMillis()).append(", ")
    // .append("\"threadName\"").append(":").append("\"").append(threadName).append("\"").append(",
    // ")
    // .append("\"totalTimeMs\"").append(":").append(System.currentTimeMillis() -
    // startTimeMs).append("ms, ")
    // .append("\"ptxId\"").append(":").append("\"").append(ptxId).append("\"").append(", ")
    // .append("\"txId\"").append(":").append("\"").append(txId).append("\"").append(", ")
    // .append("\"mkey\"").append(":").append("\"").append(mkey).append("\"").append(", ")
    // .append("\"bizKey\"").append(":").append("\"").append(bizKey).append("\"").append(", ")
    // .append("\"args\"").append(":").append("\"[").append(StringUtils.join(args, ",
    // ")).append("]\"").append(", ")
    // .append("}")
    // .toString();
    // }
}
